//
//  ZYVSModeSettingView.m
//  YiLin
//
//  Created by Zyark on 2018/6/6.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYVSModeSettingItemView.h"
#import "ZYModeTypeModel.h"
#import "ZYImageUtils.h"

@interface ZYVSModeSettingItemView()
/**
 模式图片
 */
@property(nonatomic,strong) UIImageView *modeTypeImageView;

/**
 模式名称
 */
@property(nonatomic,strong) UILabel *modeNameLabel;


/**
 风速
 */
@property(nonatomic,strong) UISegmentedControl *control;

/**
 新风阀比例
 */
@property(nonatomic,strong) UIButton *proportionBtn;

@property(nonatomic,assign) NSUInteger modeType;

@end

@implementation ZYVSModeSettingItemView

-(instancetype)initWithFrame:(CGRect)frame withModeType:(NSUInteger)modeType{
    self = [super initWithFrame:frame];
    if (self) {
        _modeType = modeType;
        [self setup];
    }
    return self;
}

-(void)setup{
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    CGFloat imageWidth = self.width * 0.13;
    //模式图片
    _modeTypeImageView = [[UIImageView alloc] init];
    _modeTypeImageView.size = CGSizeMake(imageWidth, imageWidth);
    [self addSubview:_modeTypeImageView];
    _modeTypeImageView.left = 17;
    _modeTypeImageView.top = imageWidth * 0.3;
    
    //模式名称
    _modeNameLabel = [[UILabel alloc] init];
    _modeNameLabel.textColor = kMainColor;
    _modeNameLabel.font = [UIFont systemFontOfSize:14];
    _modeNameLabel.size = CGSizeMake(imageWidth * 3, imageWidth  * 0.4);
    [self addSubview:_modeNameLabel];
    _modeNameLabel.top = _modeTypeImageView.top;
    _modeNameLabel.left = _modeTypeImageView.right + 20;
    
    NSArray *titleArray = @[[language getStringByKey:LC_HOME_WIND_SPEED_TYPE_LOW],
                            [language getStringByKey:LC_HOME_WIND_SPEED_TYPE_MIDDLE],
                            [language getStringByKey:LC_HOME_WIND_SPEED_TYPE_HEIGHT],
                            ];
    _control = [[UISegmentedControl alloc] initWithItems:titleArray];
    _control.frame = CGRectMake(0, 0, 150, imageWidth * 0.5);
    [self addSubview:_control];
    _control.bottom = _modeTypeImageView.bottom;
    _control.left = _modeNameLabel.left;
    [_control setEnabled:YES];
    _control.selectedSegmentIndex = 0;
    UIFont *font = [UIFont boldSystemFontOfSize:14.0f];
    [_control setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:font} forState:UIControlStateSelected];
    [_control setTitleTextAttributes:@{NSForegroundColorAttributeName:RGBA(153, 153, 153, 1),NSFontAttributeName:font} forState:UIControlStateNormal];
    _control.layer.cornerRadius = 5;
    _control.layer.masksToBounds = YES;
    _control.tintColor = kMainColor;
    [_control addTarget:self action:@selector(changeWindSpeedType:) forControlEvents:UIControlEventValueChanged];
    
    //新风阀比例
    if(_modeType == ZYVSModeTypeMixedWind){
        [self setupButton];
    }
}

-(void)setupButton{
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    _proportionBtn = [[UIButton alloc] init];
    _proportionBtn.size = CGSizeMake(75, _control.height);
    [self addSubview:_proportionBtn];
    _proportionBtn.left = _control.right + 5;
    _proportionBtn.centerY = _control.centerY;
    _proportionBtn.layer.cornerRadius = 5;
    _proportionBtn.layer.masksToBounds = YES;
    _proportionBtn.layer.borderColor = kMainColor.CGColor;
    _proportionBtn.layer.borderWidth = 1;
    [_proportionBtn setTitleColor:kMainColor forState:UIControlStateNormal];
    [_proportionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    
    NSString *title = [language getStringByKey:LC_QUERY_SETUP_PROPORTION_BTN_TITLE];
    [_proportionBtn setTitle:title forState:UIControlStateNormal];
    UIFont *buttonFont = [UIFont systemFontOfSize:12];
    [_proportionBtn.titleLabel setFont:buttonFont];
    NSDictionary *attributes = @{NSFontAttributeName:buttonFont,};
    CGSize size = [title boundingRectWithSize:CGSizeMake(self.width - _control.right - 10, _proportionBtn.titleLabel.height)
                                      options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    _proportionBtn.width = size.width + 20;
    [_proportionBtn setBackgroundImage:[ZYImageUtils imageWithColor:kMainColor rect:_proportionBtn.bounds] forState:UIControlStateHighlighted];
    [_proportionBtn setBackgroundImage:[ZYImageUtils imageWithColor:[UIColor whiteColor] rect:_proportionBtn.bounds] forState:UIControlStateNormal];
    [_proportionBtn addTarget:self action:@selector(doProportionClick:) forControlEvents:UIControlEventTouchUpInside];
    _proportionBtn.hidden = YES;
}

-(void)setModel:(ZYModeTypeModel *)model{
    _model = model;
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    _modeTypeImageView.image = [UIImage imageNamed:model.imageName];
    _modeNameLabel.text = [language getStringByKey:model.title];
    if(model.modeType == ZYVSModeTypeMixedWind){
        _proportionBtn.hidden = NO;
    }
}


/**
 改变风速
 @param control 当前选择器
 */
-(void)changeWindSpeedType:(UISegmentedControl *)control{
    NSInteger index =  control.selectedSegmentIndex;
    ZYWindSpeedType speedType;
    if(index == 0){
        speedType = ZYWindSpeedTypeLow;
    }else if(index == 1){
        speedType = ZYWindSpeedTypeMiddle;
    }else{
        speedType = ZYWindSpeedTypeHeight;
    }
    if([_delegate respondsToSelector:@selector(vsModeSettingItemView:changeWindSpeedType:modeType:)]){
        [_delegate vsModeSettingItemView:self changeWindSpeedType:speedType modeType:_modeType];
    }
}

-(void)doProportionClick:(UIButton *)button{
    if([_delegate respondsToSelector:@selector(vsModeSettingItemView:doProportionClick:)]){
        [_delegate vsModeSettingItemView:self doProportionClick:button];
    }
}

-(void)setWindSpeedType:(NSUInteger)windSpeedType{
    _windSpeedType = windSpeedType;
    NSInteger index = 0;
    switch (windSpeedType) {
        case ZYWindSpeedTypeLow:
            index = 0;
            break;
        case ZYWindSpeedTypeMiddle:
            index = 1;
            break;
        case ZYWindSpeedTypeHeight:
            index = 2;
            break;
    }
    [_control setSelectedSegmentIndex:index];
}

-(void)setProportion:(NSUInteger)proportion{
    _proportion = proportion;
    
}

@end
